<template>
  <div class="page-container page-footer-btn">
    <van-cell-group>
      <van-divider><span style="font-size:15px">货位信息</span></van-divider>
      <FortenScan
        ref="scanLocation"
        propPlaceholder="请扫描货位编码/识别码"
        propErrorMsg="请扫描货位编码/识别码"
        @scanCall="handleScanLocationOver"
      />
      <van-field label="名称" :value="currentLocation.name" readonly />
      <van-field label="编码" :value="currentLocation.code" readonly />
    </van-cell-group>

    <van-cell-group>
      <FortenScan
        ref="scan"
        propPlaceholder="请扫描SN/RFID"
        propErrorMsg="请扫描正确的SN/RFID"
        :propTypeArr="['sn','rfid']"
        @scanCall="handleScanOver"
        :noAutoFocus="true"
      />
    <van-divider><span style="font-size:15px">耗材信息</span></van-divider>
    <van-field label="名称" :value="form.matrName" readonly />
    <van-field label="单位" :value="form.packageUnit" readonly />
    </van-cell-group>
  </div>
</template>
<script>
import barcodeParse from '@/util/barcodeParse.js'

export default {
  name: '货位调整',
  data() {
    return {
      scan: '',
      scanLocation: '',
      form: {},
      currentLocation: {}
    }
  },
  props: {
    propFlowKey: String
  },
  components: {
  },
  methods: {
    //扫描
    handleScanOver(scanValue) {
      if (!this.currentLocation || !this.currentLocation.id) {
        this.$notify.warning('请先扫描货位')
        this.$refs.scanLocation.focus()
        return
      }
      this.utils.loadShow()
      this.$ajax.get(`/location/limit_matr/bindOrUnbind?uniqueCode=${scanValue}&locationCode=${this.currentLocation.code}&isScan=1`).then(resp => {
        if (resp.code == 0) {
          this.form = resp.data
          if (resp.msg.indexOf('解绑') != -1) {
            this.$notify.warning(resp.msg)
          } else {
            this.$notify.success(resp.msg)
          }
          this.reload()
        }
        this.utils.loadHide()
      })
    },
    handleScanLocationOver(scanValue) { //添加新的默认货位
      this.scanLocation = scanValue
      let wrapper = this.$newWrapper().eq('code', scanValue).or('idcode', scanValue)
      let params = {
        isScan: 1,
        queryString: JSON.stringify(wrapper.queryConditions)
      }
      this.$ajax.get('/base/location/list', {params}).then(resp => {
        if (resp.data && resp.data.length > 0) {
          this.currentLocation = resp.data[0]
          this.$refs.scan.focus()
          console.log('this.currentLocation', this.currentLocation)
        } else {
          window.scanError()
          this.$notify.warning('不能识别的货位编码！')
        }
        this.scanLocation = ''
      })
    },
    async bindMatrLocation() { //绑定新的货位
      if (!this.currentLocation || !this.currentLocation.id) {
        return
      }
      if (this.form.locationId == this.currentLocation.id) {
        return
      }
      let params = {
        location: this.currentLocation,
        stocks: [this.form]
      }
      const result = await this.$dialog.confirm({
        title: '调整确认',
        message: `确定要将该商品由${this.form.locationName}调整至${this.currentLocation.name}吗？`
      })
      if (!result) {
        return
      }

      this.$ajax.post('/wm/transfer/adjustLocation', params).then(resp => {
        this.$notify.success('货位调整成功')
        this.$set(this.form, 'locationId', this.currentLocation.id)
        this.$set(this.form, 'locationName', this.currentLocation.name)
      })
    },
    handleBatchAdjust() {
      this.$router.push({name: 'batchAdjust'})
    },
    reload() {
      this.$refs.scanLocation.focus()
      this.form = {}
      this.scanLocation = ''
      this.scan = ''
      this.currentLocation = {}
    }
  },
  mounted() {
  }
}
</script>

<style lang="scss" scoped>
  .van-col {
    text-align: center;
  }
</style>
